<?php
namespace app\index\model;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 库存日志类
 */
class StockLog extends Base{
     /**
      *  库存列表
      */
	public function pageQuery(){
        $shopId = input('shopId/d',0);
        if(!$shopId)$shopId = session('shopId');
        $goodsId = session('goodsId');
		$where = [];
		if($shopId)$where['s.shopId'] = $shopId;
		if($goodsId)$where['s.goodsId'] = $goodsId;
        $createTime = input('createTime');
        if($createTime!='')$where['s.createTime'] = ['like',"%$createTime%"];
        $goodsName = input('goodsName');
        if($goodsName != '')$where['g.goodsName'] = ['like',"%$goodsName%"];
        $month = input('month');
        if($month != '')$where['s.createTime'] = ['like',"%$month%"];
        $type = input('type');
        if($type != '')$where['s.type'] = $type;
		$page = Db::name('stockLog')->alias('s')->join('__GOODS__ g','g.goodsId=s.goodsId','left')
				->join('__SHOPS__ h','h.shopId=s.shopId','left')
		        ->where($where)->field('g.goodsName,h.shopName,s.*')
		        ->order('logId desc')->paginate(input('pagesize/d'))->toArray();
		if(count($page['Rows'])>0){
			foreach ($page['Rows'] as $key => $v){
  				$page['Rows'][$key]['type'] = $v['type'] ? '增加' : '减少';
			}
		}		
        $page['SumCk'] = Db::name('stockLog')->alias('s')->join('__GOODS__ g','g.goodsId=s.goodsId','left')
		        ->where($where)->where('s.type',0)->sum('num');
        $page['SumRk'] = Db::name('stockLog')->alias('s')->join('__GOODS__ g','g.goodsId=s.goodsId','left')
		        ->where($where)->where('s.type',1)->sum('num');
		return $page;
	}

	/**
	 * 新增
	 */
	public function add($goodsId,$shopId,$type=0,$num=1,$desc=''){
		$data = [];
		$data["goodsId"] = $goodsId;
		$data["shopId"] = $shopId;
		$data["type"] = $type;
		$data["num"] = $num;
		$data["desc"] = $desc;
		$data["createTime"] = date('Y-m-d H:i:s');
		if($type == 0){				
	        $goodsStock = model('stocks')->where(['goodsId'=>$data["goodsId"],'shopId'=>$data["shopId"],'dataFlag'=>1])->value('goodsStock');
	        if($goodsStock < $num)return HTRReturn('品库存不足');
		}
		Db::startTrans();
		try{
			$result = $this->allowField(true)->save($data);
			if(false === $result)throw \Exception('日志生成失败');
			if($type){		
		    	$result = model('stocks')->where(['goodsId'=>$data["goodsId"],'shopId'=>$data["shopId"],'dataFlag'=>1])->setInc('goodsStock',$num);
		    	if(false === $result)throw \Exception('库存修改失败');
				$result = model('Goods')->where(['goodsId'=>$data["goodsId"],'dataFlag'=>1])->setInc('goodsStock',$num);
				if(false === $result)throw \Exception('库存修改失败');
			}else{
		    	$result = model('stocks')->where(['goodsId'=>$data["goodsId"],'shopId'=>$data["shopId"],'dataFlag'=>1])->setDec('goodsStock',$num);	
		    	if(false === $result)throw \Exception('库存修改失败');
				$result = model('Goods')->where(['goodsId'=>$data["goodsId"],'dataFlag'=>1])->setDec('goodsStock',$num);
				if(false === $result)throw \Exception('库存修改失败');
			}
        	Db::commit();
        	return HTRReturn("操作成功", 1);
		}catch (\Exception $e) {
            Db::rollback();
            return HTRReturn('操作失败');
        }
	}
}
